package com.leo;

import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.util.Callback;

/**
 * @ClassName MyCallBackDemo
 * @Description 自定义展示类型
 * @Author wangss
 * @date 2020.06.09 22:30
 * @Version 1.0
 */
public class MyCallBackDemo<T extends PersonProperty> implements Callback<ListView<T>, ListCell<T>> {

    private PersonProperty personProperty;

    @Override
    public ListCell<T> call(ListView<T> param) {

        param.setOnEditStart(event -> {
            int index = event.getIndex();
            this.personProperty = param.getItems().get(index);
        });

        return new ListCell<T>() {

            private ImageView iv = new ImageView("file:D:\\图片保存\\小图片\\1.jpg");

            @Override
            public void startEdit() {
                super.startEdit();
                createHbox(personProperty, false);
            }

            @Override
            public void cancelEdit() {
                super.cancelEdit();
                createHbox(personProperty, true);
            }

            @Override
            protected void updateItem(T item, boolean empty) {
                super.updateItem(item, empty);
                if (!empty) {
                    createHbox(item, !empty);
                }
            }

            private <T extends PersonProperty> void createHbox(T item, boolean isLabel) {
                HBox hBox = new HBox(10);
                hBox.setAlignment(Pos.CENTER_LEFT);

                iv.setFitWidth(30d);
                iv.setFitHeight(30d);

                Button button = new Button(item.getName() + " button");
                hBox.getChildren().addAll(iv, button);

                if (isLabel) {
                    Label name = new Label(item.getName());
                    Label age = new Label(item.getAge());

                    hBox.getChildren().addAll(name, age);
                } else {
                    TextField name = new TextField(item.getName());
                    name.setPrefWidth(30d);
                    TextField age = new TextField(item.getAge());
                    age.setPrefWidth(30d);

                    addListenerForText(name, age);

                    hBox.getChildren().addAll(name, age);
                }

                this.setGraphic(hBox);
            }

            private void addListenerForText(TextField name, TextField age) {
                name.setOnKeyPressed(event -> {
                    if (event.getCode() == KeyCode.ENTER) {
                        action(name, age);
                    }
                });

                age.setOnKeyPressed(event -> {
                    if (event.getCode() == KeyCode.ENTER) {
                        action(name, age);
                    }
                });
            }

            private void action(TextField name, TextField age) {
                if (!"".equals(name.getText())) {
                    personProperty.setName(name.getText());
                }
                if (!"".equals(age.getText())) {
                    personProperty.setAge(age.getText());
                }
                this.commitEdit((T) personProperty);
            }
        };
    }
}
